##!/bin/bash
## Copyright for kilimall coporation 
## Time  2019-6-18
## Auther  jack.lei 
#
#
#
#
#
#
#|=| Check whether the port is duplicated here,
#|=| avoiding repeated startup and saving resources. 
#|=| The port is passed in through the second parameter.

if [ -z $2  ]

then 

  echo "Port char is not empty ..."
	exit 1
fi



PENDING_KILL_PORT=`netstat -plnt | awk '/'$2'/{print $7}' | awk -F "/" '{print $1}'`

echo The scanned PID is  $PENDING_KILL_PORT

if [ -n "$PENDING_KILL_PORT" ];then
    
     if [ -z $3 ];then
          
        dialog --msgbox "The current port is already occupied. Please check the port usage." 20 50
        exit 1
    else
        dialog --msgbox "Now it will forcefully close the port that is already occupied and start the new service." 20 50
        kill -9 $PENDING_KILL_PORT

    fi
else

  echo  "No port duplication, normal startup . . ." 

fi
#
#
#
#
#
#
#
#
# Gather Current Date
CURRENT=`date "+%Y-%m-%d"` 
#
# Splicing the current DUMP file path 
JVM_PATH=$HOME/jvm/dump/
DUMP_PATH=$JVM_PATH
#
#Create a dump file
if [[ -f $DUMP_PATH &&  -e $DUMP_PATH ]]
then  
   echo System overflow frequency is too high

else 
   mkdir -p $DUMP_PATH
fi 
#
#
#      
#
#Get the number of CPUs corresponding to the current server kernel
kernel_cpu=`cat /proc/cpuinfo| grep "processor"| wc -l`
echo  "The current number of server cores is $kernel_cpu"
#
#
#Caculate current server maximum memory RAM
local_memory=`free | sed -n '2p' | awk  '{print $2}'`
base_memory=`expr $local_memory / 1024 / 1024`
echo " now sys memory is $base_memory G"
#
MEMORY_SINK=`expr $base_memory / 1024 / 4` 
MEMORY_XMN=`expr $MEMORY_SINK / 3`
kernel=`expr $kernel_cpu \* 4`
#
#
#Start the JVM script
#
#
#
#
#Input parameter check here

#Input jar file validation
if [ -e target/$1 ]
then  
   echo  "normal jar ..... >>  ..... "
else 
    echo  "The directory you specified does not have a jar corresponding to the startup file."
   exit 1
fi 
#
#jar file char validation 
if [ -z $1 ]
 then  
    echo "Please pass in the name of the jar file to be executed."
    exit 1

fi
#
#
#
java -jar   target/$1  
-Xmx$MEMORY_SINKm 
-Xms$MEMORY_SINKm
-Xmn$MEMORY_XMNm
-XX:SurvivorRatio=10
#
#
#Use the latest garbage collector here
-XX:+UseG1GC
-XX:MaxTenuringThreshold=10
-XX:ParallelGCThreads=$kernel
-XX:ConcGCThreads=$kernel
-XX:+DisableExplicitGC
-XX:+HeapDumpOnOutOfMemoryError
-XX:HeapDumpPath=$DUMP_PATH
-XX:+PrintGCDetails
-XX:+PrintGCTimeStamps
-XX:+PrintHeapAtGC
-XX:+TraceClassLoading
-XX:+PrintClassHistogram








